import { mapGetters, mapActions } from "vuex";
import Checker from "vux/src/components/checker/checker.vue";
import CheckerItem from "vux/src/components/checker/checker-item.vue";
import PopupPicker from "vux/src/components/popup-picker/index.vue";

import template from "./prePaidTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  props: {
    quickBtn: {
      type: Array,
      default: function () {
        return [];
      },
    },
  },
  components: {
    Checker,
    CheckerItem,
    PopupPicker,
  },
  data() {
    return {
      checkerIndex: this.quickBtn[0],
      amountVal: null,
      prePaidData: {},

      gtbank: false,
      bankList: [], //高通网银列表
      bankListValue: [],

      confirmPay: false,
    };
  },
  created() {
    this._getBankList();
  },
  mounted() {
    this.$nextTick(() => {
      if (!this.rechargeData.payment_name) {
        this.$router.back();
      }
    });
  },
  computed: {
    ...mapGetters(["userInfo", "accountData", "rechargeData"]),
  },
  methods: {
    headBack() {
      this.$router.push({ path: "/recharge" });
    },
    payCancel() {
      this.$router.replace({
        path: "/recharge/payStatusErr",
        query: { amountVal: this.amountVal },
      });
    },
    payConfirm() {
      this.$router.replace({
        path: "/recharge/payStatus",
        query: { amountVal: this.amountVal },
      });
    },
    removeImg() {
      this.userInfo.avatar = "";
    },
    onChange(val) {
      console.log("val change", val);
    },
    _getBankList() {
      if (this.rechargeData.type_mode == 2) {
        //获取高通网银银行列表
        var params = {
          token: localStorage.getItem("token"),
          payment_id: this.rechargeData.payment_id,
        };
        this.$vux.loading.show();
        this.$http
          .post(
            this.urlRequest + "?m=api&c=recharge&a=getRechargeBankList",
            params
          )
          .then((res) => {
            this.$vux.loading.hide();
            if (res.status == 0) {
              //银行列表
              this.newData = res.bankList;
              var arr = [];
              for (var key in this.newData) {
                arr.push(this.newData[key].name);
              }
              this.bankList.push(arr);
              console.log(res.bankList);
              console.log(this.bankList);
            } else {
              if (res.ret_msg && res.ret_msg != "") {
                this.$vux.toast.show({
                  text: res.ret_msg,
                });
              }
            }
          })
          .catch((error) => {
            this.$vux.loading.hide();
            this.$vux.toast.show({
              text: "获取银行列表失败",
            });
          });
      }
    },
    payNow() {
      this.amountVal = this.$refs.amountValRef.value;
      var newAmountVal = Number(this.amountVal);
      console.log("rechargeData", this.rechargeData);
      console.log("amountVal：", this.amountVal);
      if (this.amountVal == "" || this.amountVal == 0) {
        this.$refs.amountValRef.focus();
        this.$vux.toast.show({
          text: "请输入充值金额",
        });
        return;
      }
      var formatAmount = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
      if (!formatAmount.test(this.amountVal)) {
        this.$refs.amountValRef.focus();
        this.$vux.toast.show({
          text: "输入的资金格式有误",
        });
        return;
      }
      if (this.rechargeData.line == 0) {
        //线上
        if (this.rechargeData.type_mode == 2) {
          if (this.bankListValue == "") {
            this.$vux.toast.show({
              text: "请选择银行",
            });
            return;
          }
        }
        var newLowerLimit = Number(this.rechargeData.lower_limit);
        if (newAmountVal < newLowerLimit) {
          this.$vux.toast.show({
            text: "充值金额不能小于" + newLowerLimit + "元",
          });
          return;
        }
        var params = {
          token: localStorage.getItem("token"),
          channel_type: this.rechargeData.channel_type,
          type: this.rechargeData.type,
          money: this.amountVal ? this.amountVal : "",
          pay_type: this.rechargeData.payment_name,
          bank_code: this.bankListVal,
        };
        console.log(this.rechargeData);
        console.log(params);

        if (this.rechargeData.type_mode == 0) {
          this.$vux.loading.show();
          this.$http
            .post(
              this.urlRequest + "?m=api&c=recharge&a=rechargeOnline",
              params
            )
            .then((res) => {
              this.$vux.loading.hide();
              if (res.status == 0) {
                this.order_no = res.order_no;
                this.collectBank = res.type;
                if (res.code_img) {
                  this.codeImg = res.code_img;
                  this.$router.push({
                    path: "/recharge/payment",
                    query: {
                      order: this.order_no,
                      codeImg: this.codeImg,
                      collectBank: this.collectBank,
                      amountVal: this.amountVal,
                    },
                  });
                } else {
                  this.codeUrl = res.code_url;
                  this.$router.push({
                    path: "/recharge/payment",
                    query: {
                      order: this.order_no,
                      codeUrl: this.codeUrl,
                      collectBank: this.collectBank,
                      amountVal: this.amountVal,
                    },
                  });
                }
              } else {
                this.$router.replace({
                  path: "/recharge/payStatusErr",
                  query: {
                    order: this.order,
                    code: this.code,
                    amountVal: this.amountVal,
                  },
                });
                if (res.ret_msg && res.ret_msg != "") {
                  this.$vux.toast.show({
                    text: res.ret_msg,
                  });
                }
              }
            })
            .catch((error) => {
              this.$vux.loading.hide();
              this.$vux.toast.show({
                text: "数据请求超时",
              });
            });
        } else if (this.rechargeData.type_mode == 1) {
          this.paymentLink =
            this.urlRequest +
            "?m=api&c=recharge&a=rechargeOnline" +
            "&token=" +
            localStorage.getItem("token") +
            "&channel_type=" +
            this.rechargeData.channel_type +
            "&type=" +
            this.rechargeData.type +
            "&pay_type=" +
            this.rechargeData.payment_name +
            "&money=" +
            this.amountVal;
          this.confirmPay = true;
          window.open(this.paymentLink);
        } else if (this.rechargeData.type_mode == 2) {
          this.paymentLink =
            this.urlRequest +
            "?m=api&c=recharge&a=rechargeOnline" +
            "&token=" +
            localStorage.getItem("token") +
            "&channel_type=" +
            this.rechargeData.channel_type +
            "&type=" +
            this.rechargeData.type +
            "&pay_type=" +
            this.rechargeData.payment_name +
            "&money=" +
            this.amountVal +
            "&bank_code=" +
            this.bankListVal;
          this.confirmPay = true;
          window.open(this.paymentLink);
        }
      } else {
        //线下
        var newMinRecharge = Number(this.rechargeData.min_recharge);
        var newMaxRecharge = Number(this.rechargeData.max_recharge);
        if (newAmountVal < newMinRecharge) {
          this.$vux.toast.show({
            text: "充值金额不能小于" + newMinRecharge + "元",
          });
          return;
        }
        if (newAmountVal > newMaxRecharge) {
          this.$vux.toast.show({
            text: "充值金额不能大于" + newMaxRecharge + "元",
          });
          return;
        }
        var params = {
          token: localStorage.getItem("token"),
          id: this.rechargeData.payment_id,
          money: this.amountVal ? this.amountVal : "",
        };
        this.$vux.loading.show();
        this.$http
          .post(this.urlRequest + "?m=api&c=recharge&a=rechargeOffline", params)
          .then((res) => {
            this.$vux.loading.hide();
            if (res.status == 0) {
              this.$router.push({
                path: "/recharge/payment",
                query: {
                  order: res.order_sn,
                  code: res.code,
                  amountVal: this.amountVal,
                },
              });
            } else {
              if (res.ret_msg && res.ret_msg != "") {
                this.$vux.toast.show({
                  text: res.ret_msg,
                });
              }
            }
          })
          .catch((error) => {
            this.$vux.loading.hide();
            this.$vux.toast.show({
              text: "数据请求超时",
            });
          });
      }
    },
  },
  watch: {
    bankListValue(val) {
      for (var key in this.newData) {
        if (this.newData[key].name == val) {
          this.bankListVal = this.newData[key];
        }
      }
    },
  },
};

// WEBPACK FOOTER //
// ./src/components/wallet/recharge/prePaid.vue
